var dataTableConfig = {
		"processing" : true,
		"serverSide" : true,
		"paging" : true,
		"lengthChange" : false,
		"searching" : false,
		"ordering" : false,
		"info" : true,
		"autoWidth" : false,
		"oLanguage" : {
			"sProcessing" : "正在加载中......",
			"sLengthMenu" : "每页显示 _MENU_ 条记录",
			"sZeroRecords" : "对不起，查询不到相关数据！",
			"sEmptyTable" : "表中无数据存在！",
			"sInfo" : "当前显示 _START_ 到 _END_ 条，共 _TOTAL_ 条记录",
			"sInfoFiltered" : "数据表中共为 _MAX_ 条记录",
			"sSearch" : "搜索",
			"oPaginate" : {
				"sFirst" : "首页",
				"sPrevious" : "上一页",
				"sNext" : "下一页",
				"sLast" : "末页"
			}
		},
		"oTableTools" : {
			"sRowSelect" : "multi",
			"aButtons" : [
					{
						"sExtends" : "new_record",
						"sButtonText" : "Add"
					},
					{
						"sExtends" : "select",
						"sButtonText" : "Delete Recods",
						"fnClick" : function(nButton,
								oConfig, oFlash) {
							//delete stuff comes here 
							alert('test');
						}

					} ]
		},
		ajax : "/device_type/queryDeviceType",
		columns : [
				{
					data : "type_id",
					render : function(data, type, full) {
						return '<input name=\"items\" type="checkbox" id=\"check_'+data+'\" value="' + data + '"/>';
					}
				}, {
					"data" : "type_id"
				},{
					"data" : "code"
				}, {
					"data" : "name"
				}, {
					"data" : "parent_name"
				}, {
					"data" : "model_code"
				},{
					"data": "parent_id",
				},{
					"data": "is_air",
				}],
		columnDefs: [    
		        {    
		        	"targets": [6,7], //隐藏第6、7列，从第0列开始   
		        	"visible": false    
		        }], 
		"dom" : "<'row'<'col-xs-2'l><'#mytool.col-xs-4'><'col-xs-6'f>r>"
				+ "t"
				+ "<'row'<'col-xs-6'i><'col-xs-6'p>>",
};

//初始化表格
var table = $('#device_type_table').DataTable(dataTableConfig);

//全选
$(function() {
	//全选
	$("#checkall_device_type").click(function() {
		$('[name=items]:checkbox').prop("checked", this.checked);
	});
	$('[name=items]:checkbox').click(
		function() {
			var $tmp = $('[name=items]:checkbox');
			$('#checkall_device_type').prop('checked',
					$tmp.length == $tmp.filter(':checked').length);
		}	
	);
});

var app = angular.module('deviceTypeApp', []);

var initController = app.controller("deviceTypeController" , function($scope, $http){
	$('#addModal_device_type').on('hide.bs.modal', function () {
		document.getElementById("addForm_device_type").reset(); 
	});
	$http({
		url : '/device_type/getDeviceTypeList',
		method : 'GET'
	}).success(function(data, header, config, status) {
		// 响应成功
		$scope.deviceTypeList = data;
	}).error(function(data, header, config, status) {
		// 处理响应失败
	});
	$scope.add = function(){
		var form = document.getElementById("addForm_device_type");
		var code = form.code_device_type.value;
		var name = form.name_device_type.value;
		var parent_id = form.parent_id_device_type.value;
		var model_code = form.model_code_device_type.value;
		var is_air = form.is_air_device_type.value;
		$http({
			url : '/device_type/add',
			params:{code:code, name:name, parent_id:parent_id , model_code:model_code, is_air:is_air},
			method : 'GET'
		}).success(function(data, header, config, status) {
			// 响应成功
			if(data=="1"){
				$('#addSuccessAlert_device_type').fadeIn(2000);
				$('#addSuccessAlert_device_type').fadeOut(2000);
				table.draw();
			}else{
				$('#addFailedAlert_device_type').fadeIn(2000);
				$('#addFailedAlert_device_type').fadeOut(2000);
			}
		}).error(function(data, header, config, status) {
			// 处理响应失败
			$('#addFailedAlert_device_type').fadeIn(2000);
			$('#addFailedAlert_device_type').fadeOut(2000);
		});
	};
	$scope.del_init=function(){
		var typeIds = "";
		$('[name=items]:checkbox:checked').each(function() {
			typeIds += $(this).val() + ";";
		});
		if(typeIds==""){
			$('#selAlert_device_type').html("<strong>请选择记录进行删除！</strong>");
			$('#alertModal_device_type').modal("toggle");
		}else{
			$("#delModal_device_type").modal("toggle");
		}
		
	};
	$scope.del = function(){
		var typeIds = "";
		$('[name=items]:checkbox:checked').each(function() {
			typeIds += $(this).val() + ";";
		});
		if(typeIds==""){
			alert("请选择要删除的记录！");
		}else{
			$http({
				url : '/device_type/delete',
				params:{typeIds:typeIds},
				method : 'GET'
			}).success(function(data, header, config, status) {
				// 响应成功
				if(data==true){
					$('#promptContent_device_type').hide();
					$('#del_device_type').hide();
					$('#deleteSuccessAlert_device_type').fadeIn(2000);
					$('#delModal_device_type').on('hide.bs.modal', function () {
						table.draw();
						$('#del_device_type').show();
						$('#deleteSuccessAlert_device_type').hide();
						$('#promptContent_device_type').show();
					});
				}else{
					$('#promptContent_device_type').hide();
					$('#del_device_type').hide();
					$('#deleteFailedAlert_device_type').fadeIn(2000);
					$('#delModal_device_type').on('hide.bs.modal', function () {
						table.draw();
						$('#del_device_type').show();
						$('#deleteFailedAlert_device_type').hide();
						$('#promptContent_device_type').show();
					});
				}
				
			}).error(function(data, header, config, status) {
				// 处理响应失败
				$('#promptContent_device_type').hide();
				$('#del_device_type').hide();
				$('#deleteFailedAlert_device_type').fadeIn(2000);
				$('#delModal_device_type').on('hide.bs.modal', function () {
					$('#del_device_type').show();
					$('#deleteFailedAlert_device_type').hide();
					$('#promptContent_device_type').show();
				});
			});
		}
	};
	$scope.update_init =function(){
		var typeIds = "";
		$('[name=items]:checkbox:checked').each(function() {
			typeIds += $(this).val() + ";";
		});
		if(typeIds==""){
			$('#selAlert_device_type').html("<strong>请选择要修改的记录！</strong>");
			$('#alertModal_device_type').modal("toggle");
		}else if(typeIds.split(";").length>2){
			$('#selAlert_device_type').html("<strong>修改的记录数不能大于1.</strong>");
			$('#alertModal_device_type').modal("toggle");
		}else{
			  
			 $("table :checkbox").each(function(key,value){
		            if($(value).prop('checked')){
		            	var selRow =$('#device_type_table').dataTable().fnGetData(key-1);
		            	$scope.type_id_update = selRow.type_id;
		            	$scope.code_update = selRow.code;
		            	$scope.name_update = selRow.name;
		            	$scope.parent_id_update = selRow.parent_id;
		            	$scope.parent_name_update = selRow.parent_name;
		            	$scope.model_code_update = selRow.model_code;
		            	$scope.is_air_update = selRow.is_air;
		            }
			 });
			 $("#updateModal_device_type").modal("toggle");
		}
	};
	$scope.update = function(){
		var form = document.getElementById("updateForm_device_type");
		var typeId = form.type_id_update_device_type.value;
		var code = form.code_update_device_type.value;
		var name = form.name_update_device_type.value;
		var parent_id = form.parent_id_update_device_type.value;
		var model_code = form.model_code_update_device_type.value;
		var is_air = form.is_air_update_device_type.value;
		$http({
			url : '/device_type/edit',
			params:{typeId:typeId, code:code, name:name, parent_id:parent_id , model_code:model_code, is_air:is_air},
			method : 'GET'
		}).success(function(data, header, config, status) {
			// 响应成功
			if(data=="1"){
				$('#updateSuccessAlert_device_type').fadeIn(2000);
				$('#updateSuccessAlert_device_type').fadeOut(2000);
				table.draw();
			}else{
				$('#updateFailedAlert_device_type').fadeIn(2000);
				$('#updateFailedAlert_device_type').fadeOut(2000);
			}
		}).error(function(data, header, config, status){
			// 处理响应失败
			$('#updateFailedAlert_device_type').fadeIn(2000);
			$('#updateFailedAlert_device_type').fadeOut(2000);
		});
	}
	
	$scope.query = function () {
		var url = '/device_type/queryDeviceType';
		var data = {};
		var form =document.getElementById("queryForm_device_type");  // $('#queryForm');
		if(form.code_query_device_type.value!=0){
			data.code =  form.code_query_device_type.value;
		}
		if(form.name_query_device_type.value!=0){
			data.name = form.name_query_device_type.value;
		}
		if(form.parent_id_query_device_type.value!=0){
			data.parent_id =  form.parent_id_query_device_type.value;
		}
		if(form.model_code_query_device_type.value!=0){
			data.model_code =  form.model_code_query_device_type.value;
		}
		data.is_air =  form.is_air_query_device_type.value;
		$('#queryBtn_device_type').attr("disabled", "true");
		$('#device_type_table').dataTable().api().destroy();
		dataTableConfig.ajax={"url": url, "data":data};
		dataTableConfig.fnDrawCallback = function(settings){
			$('#queryBtn_device_type').removeAttr("disabled");
		};
		table=$('#device_type_table').dataTable(dataTableConfig);
	};
});